﻿using HZY.Framework.DependencyInjection;
using Long.BLL;
using Long.DAL;
using Long.Model;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using SqlSugar;
using System.Reflection;

namespace Long.SpraySCADA
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.

            

            //创建服务容器
            var services = new ServiceCollection();
            //注册服务,,核心服务在这里注册
            ConfigureService(services);
            //创建服务提供者
            var serviceProvider = services.BuildServiceProvider();
            //将服务提供者注入到全局变量中
            Globals.ServiceProvider = serviceProvider;

            ApplicationConfiguration.Initialize();

                //ISqlSugarClient  优先于FrmMain 注册 解决 db=null问题
            var db = Globals.ServiceProvider.GetRequiredService<ISqlSugarClient>();
            var frmMain = serviceProvider.GetRequiredService<FrmMain>();

            

            #if DEBUG 
            //debug 情况下执行
            /***手动建多个表***/
            db.CodeFirst.SetStringDefaultLength(200)
            .InitTables(typeof(AuthEntity), typeof(BaseEntity), typeof(DataEntity), typeof(RecipeEntity), typeof(UserEntity));
            #endif

            Application.Run(frmMain);
        }

        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="services"></param>
        private static void ConfigureService(ServiceCollection services)
        {
            //注册依赖注入，通过程序集的 某一个类 自动注册程序集内的类。 条件是 该类实现了 比如 ISingletonSelfDependency 之类的接口
            services.AddDependencyInjection(new List<Assembly>()
                {
                    typeof(Program).Assembly,
                    typeof(BaseDto).Assembly,
                    typeof(DB).Assembly,

                });

            #region 配置NLog

            //注册json配置
            IConfigurationBuilder cfgBuilder = new ConfigurationBuilder()
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("appsettings.json");

            //构建json配置
            IConfiguration configuration = cfgBuilder.Build();
            //注入json单例对象
            services.AddSingleton<IConfiguration>(configuration)
                .AddLogging(loggerBuilder => {
                    loggerBuilder.ClearProviders();//清除其他日志的提供者
                    loggerBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);//设置等级
                    loggerBuilder.AddNLog();
                });

            //获取NLog配置信息
            var nlogConifg = configuration.GetSection("NLog");

            //设置Nlog配置
            LogManager.Configuration = new NLogLoggingConfiguration(nlogConifg);

            #endregion

            #region 注入数据库连接
            DbType dbType = Enum.Parse<DbType>(configuration["DbContexts:DbType"]);
            string connectionString = configuration[$"DbContexts:{dbType}:ConnectionString"];
            services.AddSqlsugarSetup(dbType, connectionString);
            #endregion
        }
    }
}